//
//  LPActivityDetailDetailsFooterView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/7/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailDetailsFooterView.h"
/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#import "ZJStudyRecommendModel.h"
#import "LPActivityDetailTableHeaderView.h"
#import "LPActivityDetailTableFooterView.h"
@interface LPActivityDetailDetailsFooterView ()<UITableViewDelegate,UITableViewDataSource,LPActivityDetailFooterDelegate>
/**  tableview  */
@property (strong, nonatomic) UITableView  *tableView;

@end
@implementation LPActivityDetailDetailsFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutUI];
    }
    return self;
}

- (void)setStudyArray:(NSArray *)studyArray{
    _studyArray = studyArray;
    CGFloat tableHeight = 0.0;
    for (ZJStudyRecommendModel *model in studyArray) {
        if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
          tableHeight += 125 * [UIScreen zj_screenHScale];//图书
        }else{
            tableHeight += 120 * [UIScreen zj_screenHScale];
        
        }
        
    }
    
    if (studyArray.count != 0) {
        tableHeight += 110 * [UIScreen zj_screenHScale];
        [self addFooterAndHeader];
    }
    
    CGRect frame = self.tableView.frame;
    frame.size.height = tableHeight;
    self.tableView.frame = frame;
    [self.tableView reloadData];
    
}

#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    
}


#pragma mark - 私有方法
- (void)addFooterAndHeader{
    LPActivityDetailTableHeaderView *heaer = [[LPActivityDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale]) buttonTitle:@"学习" buttonImgae:@"wxy_jiaoliu_l"];
    self.tableView.tableHeaderView = heaer;
    
    LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
    footer.titles = @[@"查看更多学习"];
    footer.delegate = self;
    self.tableView.tableFooterView = footer;


}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    /**  查看更多的点击方法  */
    if (self.delegate) {
        [self.delegate LPActivityDetailDetailsFooterStudy_wactchMore];
    }
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.studyArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJStudyRecommendModel *model = self.studyArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频

}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ZJStudyRecommendModel * model = self.studyArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyBookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else{
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate) {
        [self.delegate LPActivityDetailDetailsFooterStudy_tapIndex:indexPath.row];
    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], self.bounds.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}



@end
